package com.example.demo.order.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.order.entity.OCardManage;
import com.example.demo.order.service.OCardManageService;
import com.example.demo.util.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 卡券管理表; 前端控制器
 * </p>
 *
 * @author 涛
 * @since 2024-08-01
 */
@RestController
@RequestMapping("/oCardManage")
@Tag(name = "oCardManage", description = "卡券管理表")
public class OCardManageController {

    @Autowired
    private OCardManageService oCardManageService;

    /**
     * 获取卡券管理列表
     */
    @GetMapping("/getCardList")
    @Operation(summary = "获取卡券管理列表", description = "获取卡券管理列表")
    public Result<?> getCardList(@RequestParam(value = "current", required = false, defaultValue = "1")Integer current,
                                 @RequestParam(value = "size", required = false, defaultValue = "10")Integer size,
                                 OCardManage card){
        Page<OCardManage> page = new Page<>(current,size);
        QueryWrapper<OCardManage> queryWrapper = new QueryWrapper<>(card);
        return Result.ok(oCardManageService.page(page,queryWrapper));
    }

    /**
     * 根据ID查询卡券管理信息
     */
    @GetMapping("/getCardById")
    @Operation(summary = "根据ID查询卡券管理信息", description = "根据ID查询卡券管理信息")
    public Result<?> getCardById(Integer id){
        return Result.ok(oCardManageService.getById(id));
    }

    /**
     * 修改卡券管理信息
     */
    @PutMapping("/updateCard")
    @Operation(summary = "根据id修改卡券管理信息", description = "根据id修改卡券管理信息")
    @Transactional
    public Result<?> updateCard(@RequestBody OCardManage card){
        return Result.ok(oCardManageService.updateById(card));
    }

    /**
     * 新增卡券管理信息
     */
    @PostMapping("/addCard")
    @Operation(summary = "新增卡券管理信息", description = "新增卡券管理信息")
    public Result<?> addCard(@RequestBody OCardManage card){
        return Result.ok(oCardManageService.save(card));
    }

    /**
     * 删除卡券管理
     */
    @DeleteMapping("/deleteCardById")
    @Operation(summary = "删除卡券管理", description = "删除卡券管理")
    @Transactional
    public Result<?> deleteCardById(Integer id){
        return Result.ok(oCardManageService.removeById(id));
    }

    /**
     * 获取用户卡券数量
     */
    @GetMapping("/getCardCount")
    @Operation(summary = "获取用户卡券数量", description = "获取用户卡券数量")
    public Result<?> getCardCount(Integer userId){
        return oCardManageService.getCardCount(userId);
    }
}

